/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package demo;

import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author CodeBlue
 */
public class CustomTableModel extends AbstractTableModel {

    public final static boolean GENDER_MALE = true;
    public final static boolean GENDER_FEMALE = false;
    public final static String[] columnNames = {
        "First Name", "Last Name", "Date of Birth", "Account Balance", "Gender"
    };
    public Object[][] values = {
        {
            "Clay", "Ashworth",
            new GregorianCalendar(1962, Calendar.FEBRUARY, 20).getTime(),
            new Float(12345.67), GENDER_MALE}, {
            "Jacob", "Ashworth",
            new GregorianCalendar(1987, Calendar.JANUARY, 6).getTime(),
            new Float(23456.78), GENDER_MALE}, {
            "Jordan", "Ashworth",
            new GregorianCalendar(1989, Calendar.AUGUST, 31).getTime(),
            new Float(34567.89), GENDER_FEMALE}, {
            "Evelyn", "Kirk",
            new GregorianCalendar(1945, Calendar.JANUARY, 16).getTime(),
            new Float(-456.70), GENDER_FEMALE}, {
            "Belle", "Spyres",
            new GregorianCalendar(1907, Calendar.AUGUST, 2).getTime(),
            new Float(567.00), GENDER_FEMALE}
    };

    @Override
    public int getRowCount() {
        return values.length;
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return values[rowIndex][columnIndex];
    }
    
    /*
     * JTable uses this method to determine the default renderer/
     * editor for each cell.  If we didn't implement this method,
     * then the last column would contain text ("true"/"false"),
     * rather than a check box.
     */
    @Override
    public Class getColumnClass(int c) {
        if (getValueAt(0, c) == null) {
            return String.class;
        }
        return getValueAt(0, c).getClass();
    }

    // Need for showing column name
    @Override
    public String getColumnName(int columnIndex) {
        return columnNames[columnIndex];
    }

    /*
     * Don't need to implement this method unless your table's
     * editable.
     */
    @Override
    public boolean isCellEditable(int row, int col) {
        //Note that the data/cell address is constant,
        //no matter where the cell appears onscreen.
        return col != 2;
    }

    @Override
    public void setValueAt(Object value, int row, int col) {
        if (value.toString().length() < 3) {
            JOptionPane.showMessageDialog(null, "Must be more than 3 letters");
            return;
        }
        values[row][col] = value;
        fireTableCellUpdated(row, col);

    }
}
